home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Pascal / Snippets / PNL Libraries / MyHandleFile.p < prev    next >
Encoding:
Text File  |  1997-04-15  |  2.8 KB  |  122 lines  |  [TEXT/CWIE]

  1. unit MyHandleFile;
  2.  
  3. interface
  4.  
  5.     uses
  6.         Types, MyTypes;
  7.  
  8.     type
  9.         HandleFile = record
  10.                 data: Handle; { The data }
  11.                 pos: longint; { current position in Handle }
  12.                 crlf: CRLFTypes; { Only used for output, reading will Handle either case }
  13.                 error: OSErr; { Cumulative error }
  14.             end;
  15. { You are free to modify any and all fields, keep 0<=pos<MGetHandleSize(data) }
  16.  
  17.     procedure CreateHandleFile (var hf: HandleFile; le: CRLFTypes);
  18.     procedure CreateHandleFileFromHandle (var hf: HandleFile; data: Handle; le: CRLFTypes);
  19.     procedure DestroyHandleFile (var hf: HandleFile);
  20.     procedure WriteToHandleFile (var hf: HandleFile; s: Str255); { Write string at pos into data }
  21.     function ReadLongLineFromHandle (var hf: HandleFile; var off, len: longint): boolean;
  22.     function ReadFromHandleFile (var hf: HandleFile; var s: Str255): boolean;
  23. { Read string from pos in data, update pos, return true unless eohandle }
  24.  
  25. implementation
  26.  
  27.     uses
  28.         Memory, ToolUtils, TextUtils,
  29.         MyMemory;
  30.  
  31.     procedure CreateHandleFileFromHandle (var hf: HandleFile; data: Handle; le: CRLFTypes);
  32.     begin
  33.         hf.data := data;
  34.         hf.pos := 0;
  35.         hf.crlf := le;
  36.         hf.error :=noErr;
  37.     end;
  38.     
  39.     procedure CreateHandleFile (var hf: HandleFile; le: CRLFTypes);
  40.     begin
  41.         hf.error := MNewHandle(hf.data, 0);
  42.         hf.pos := 0;
  43.         hf.crlf := le;
  44.     end;
  45.  
  46.     procedure DestroyHandleFile (var hf: HandleFile);
  47.     begin
  48.         MDisposeHandle(hf.data);
  49.     end;
  50.  
  51.     procedure WriteToHandleFile (var hf: HandleFile; s: Str255);
  52.         var
  53.             ret: longint;
  54.     begin
  55.         with hf do begin
  56.             case crlf of
  57.                 CL_CR: 
  58.                     s := concat(s, cr);
  59.                 CL_LF: 
  60.                     s := concat(s, lf);
  61.                 CL_CRLF: 
  62.                     s := concat(s, cr, lf);
  63.             end;
  64.             ret := MMungerInsertString( data, pos, s );
  65.             if ret >= 0 then begin
  66.                 pos := ret;
  67.             end;
  68.             if error = noErr then begin
  69.                 error := MemError;
  70.             end;
  71.         end;
  72.     end;
  73.  
  74.     function ReadLongLineFromHandle (var hf: HandleFile; var off, len: longint): boolean;
  75.         var
  76.             size: longint;
  77.             p: Ptr;
  78.     begin
  79.         with hf do begin
  80.             size := MGetHandleSize(data);
  81.             ReadLongLineFromHandle := pos < size;
  82.             off := pos;
  83.             p := Ptr(ord(data^) + pos);
  84.             while (pos < size) & (p^ <> 13) & (p^ <> 10) do begin
  85.                 pos := pos + 1;
  86.                 longint(p) := longint(p) + 1;
  87.             end;
  88.             len := pos - off;
  89.             if (pos < size) & (p^ = 13) then begin
  90.                 pos := pos + 1;
  91.                 longint(p) := longint(p) + 1;
  92.             end;
  93.             if (pos < size) & (p^ = 10) then begin
  94.                 pos := pos + 1;
  95.                 longint(p) := longint(p) + 1;
  96.             end;
  97.         end;
  98.     end;
  99.  
  100.     function ReadFromHandleFile (var hf: HandleFile; var s: Str255): boolean;
  101.         var
  102.             off, len: longint;
  103.             p: Ptr;
  104.             gotline: boolean;
  105.     begin
  106.         s := '';
  107.         gotline := ReadLongLineFromHandle(hf, off, len);
  108.         if gotline then begin
  109.             p := Ptr(ord(hf.data^) + off);
  110.             if len > 255 then begin
  111.                 len := 255;
  112.             end;
  113. {$PUSH}
  114. {$R-}
  115.             s[0] := chr(len);
  116.             BlockMoveData(p, @s[1], len);
  117. {$POP}
  118.         end;
  119.         ReadFromHandleFile := gotline;
  120.     end;
  121.  
  122. end.